package cn.smbms.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import cn.smbms.dao.ProviderMapper;
import cn.smbms.pojo.Provider;
import cn.smbms.utils.MyBatisUtil;
import cn.smbms.utils.PageUtil;

public class ProviderService {
	public static final String PAGEPATH = "/WEB-INF/jsp/";

	public void getProviderList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		String proName = request.getParameter("proName");
		Integer currentPage = 1;
		try {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		} catch (NumberFormatException e) {
		}
		List<Provider> providerList = sqlSession.getMapper(ProviderMapper.class).getPageByProName(proName, currentPage);
		int count = sqlSession.getMapper(ProviderMapper.class).count(proName);
		MyBatisUtil.closeSqlSession(sqlSession);
		PageUtil page = new PageUtil();
		page.setTotalCount(count);
		page.setCurrentPage(currentPage);
		page.setRows(providerList);
		request.setAttribute("page", page);
		request.setAttribute("proName", proName);
		request.getRequestDispatcher(PAGEPATH + "providerList.jsp").forward(request, response);

	}

	public void getProviderById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		Integer providerId = null;
		try {
			providerId = Integer.parseInt(request.getParameter("providerId"));
		} catch (NumberFormatException e) {
		}
		Provider provider = sqlSession.getMapper(ProviderMapper.class).getProviderById(providerId);
		MyBatisUtil.closeSqlSession(sqlSession);
		request.setAttribute("provider", provider);
		String opr = request.getParameter("opr");
		request.getRequestDispatcher(PAGEPATH + "provider" + opr + ".jsp").forward(request, response);

	}

	public void addProvider(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		String opr = request.getParameter("opr");
		if (opr.equals("show")) {
			request.getRequestDispatcher(PAGEPATH + "providerAdd.jsp").forward(request, response);
			return;
		} else if (opr.equals("add")) {
			String proCode = request.getParameter("proCode");
			String proName = request.getParameter("proName");
			String proContact = request.getParameter("proContact");
			String proPhone = request.getParameter("proPhone");
			String proAddress = request.getParameter("proAddress");
			String proFax = request.getParameter("proFax");
			String proDesc = request.getParameter("proDesc");
			Provider p = new Provider(proCode, proName, proDesc, proContact, proPhone, proAddress, proFax);
			int i = sqlSession.getMapper(ProviderMapper.class).addProvider(p);
			MyBatisUtil.closeSqlSession(sqlSession);
			response.getWriter().print("<script>alert('账单添加" + (i == 1 ? "成功" : "失败")
					+ "！');location.href='provider-getProviderList.do';</script>");

		}

	}

	public void updateProvider(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		Integer id = null;
		try {
			id = Integer.parseInt(request.getParameter("providerId"));
		} catch (NumberFormatException e) {
		}
		String proCode = request.getParameter("proCode");
		String proName = request.getParameter("proName");
		String proContact = request.getParameter("proContact");
		String proPhone = request.getParameter("proPhone");
		String proAddress = request.getParameter("proAddress");
		String proFax = request.getParameter("proFax");
		String proDesc = request.getParameter("proDesc");
		Provider p = new Provider(id, proCode, proName, proDesc, proContact, proPhone, proAddress, proFax);
		int i = sqlSession.getMapper(ProviderMapper.class).updateProvider(p);
		MyBatisUtil.closeSqlSession(sqlSession);

		response.getWriter().print("<script>alert('账单修改" + (i == 1 ? "成功" : "失败")
				+ "！');location.href='provider-getProviderList.do';</script>");

	}

	public void deleteProvider(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		Integer providerId = null;
		try {
			providerId = Integer.parseInt(request.getParameter("providerId"));
		} catch (NumberFormatException e) {
		}
		int i = sqlSession.getMapper(ProviderMapper.class).deleteProvider(providerId);
		// int i = 1;
		MyBatisUtil.closeSqlSession(sqlSession);
		response.getWriter().print("<script>alert('账单删除" + (i == 1 ? "成功" : "失败")
				+ "！');location.href='provider-getProviderList.do';</script>");

	}
}
